package app.auto.runner.base.utility;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.widget.AppCompatEditText;

import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aliang.auto.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import app.auto.AndroidInstance;
import app.auto.annotation.Show;
import app.auto.runner.base.BroadcastBuilder;
import app.auto.runner.base.DialogUtil;
import app.auto.runner.base.DipUtil;
import app.auto.runner.base.InputMethodUtil;
import app.auto.runner.base.OneListView;
import app.auto.runner.base.intf.FunCallback;
import app.auto.runner.base.ui.TextViewBuilder;
import app.auto.runner.base.utility.pinyin.PinyinHelper;
import app.auto.runner.clip.ClipUtil;


/**
 * Created by hp on 2018/2/22.
 */


//http

//josonparser

public class ShareAlert extends AndroidInstance {
    private FunCallback showfunc;
    private boolean editableEnabled;

    //shareEdit
    private FunCallback doSelect;
    private ShareAlert host;
    private boolean closeActy;
    private String emptystr = "";
    private String title;
    private SlideBottomUtil slideBottomUtil;
    private String searchedithint;
    //============
    private View view;
    private String model;
    private boolean defaultClose;
    private int list_layoutid = 0;
    private OneListView oneListView;
    private boolean cancelshare;

    public OneListView getOneListView() {
        return oneListView;
    }

    public ShareAlert setList_layoutid(int list_layoutid) {
        this.list_layoutid = list_layoutid;
        return this;
    }

    public ShareAlert setTitle(String title) {
        this.title = title;
        return this;
    }

    public ShareAlert setSearchEditHint(String searchedithint) {
        this.searchedithint = searchedithint;
        return this;
    }

    public ShareAlert setFlag_CloseActy(boolean closeActy) {
        this.closeActy = closeActy;
        return this;
    }

    public ShareAlert setDoSelect(FunCallback doSelect) {
        this.doSelect = doSelect;
        return this;
    }


    public ShareAlert setEditableDirected(boolean editableEnabled) {
        this.editableEnabled = editableEnabled;
        return this;
    }

    @Show
    public static void shareAlert(final Activity ctx, final String url, final String params, final String result, final String title) {
        shareAlertPrivate(ctx, url, params, result, title);

    }

    private static void shareAlertPrivate(final Activity ctx, final String url, final String params, final String result, final String title) {
        ctx.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ShareAlert.shareAlert(ctx, "URL: \n" + url + "\n" + "Params: \n" + params + "\nResult: \n" + result + "\n" + title);
            }
        });
    }

    public Dialog getDialog() {
        return dialog;
    }

    public boolean cliboard = false;

    public ShareAlert setSetCliboard(boolean setCliboard) {
        this.cliboard = setCliboard;
        return this;
    }


    public List<String> flowLayoutLabel = new ArrayList();

    public ShareAlert setFlowLayoutLabel(List<String> flowLayoutLabel) {
        this.flowLayoutLabel = flowLayoutLabel;
        return this;
    }

    AlertDialog editDialog;
    private boolean popEnable;
    private Drawable editBackground;

    MyClickListener.MyClickCallBack myClickCallBack;

    public MyClickListener.MyClickCallBack getMyClickCallBack() {
        return myClickCallBack;
    }

    //    public FunCallback editlongclick;
//
//    public ShareAlert setEditlongclick(FunCallback editlongclick) {
//        this.editlongclick = editlongclick;
//        return this;
//    }

    public void doubleClickPublic(final EditText edit) {
        doubleClickPrivate(edit);
    }

    private void doubleClickPrivate(final EditText edit) {
        if (!popEnable) {
            popEnable = true;
            actDoubleClick(edit, editBackground);
            //                                                    edit.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            //                                                        @Override
            //                                                        public void onGlobalLayout() {
            //
            //                                                        }
            //                                                    });
        } else {
            popEnable = false;
            InputMethodUtil.toogle(getContext(), edit);
            edit.setCursorVisible(false);
            ((TextView) editDialog.findViewById(android.R.id.button2)).setText("  ");

        }
    }

    public static void actDoubleClick(final EditText edit, final Drawable editBackground) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (editBackground != null) {
                    edit.setBackground(editBackground);
                }
                doubleCLickInner(edit);
            }
        }, 300);
    }

    public static void doubleCLickInner(EditText edit) {
        edit.setCursorVisible(true);
        edit.requestFocus();
        edit.requestFocusFromTouch();
        edit.setSelection(edit.getText().length());
//                    ((TextView) editDialog.findViewById(android.R.id.button2)).setText("应用");
        InputMethodManager inputManager = (InputMethodManager) edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(edit, 0);//展示软键盘
    }

    public void Demo() {
//        ShareAlert.get(ShareAlert.class, getContext()).shareEdit(false, (String) ((Map.Entry) getItem(position)).getValue(), "编辑", new FunCallback(){
//            @Override
//            public void onCallback(Object result, Object object) {
//                super.onCallback(result, object);
//                ((Map.Entry) getItem(position)).setValue(((EditText)object).getText().toString());
//                baseadapter.notifyDataSetChanged();
//            }
//        }, "复制", new FunCallback() {
//            @Override
//            public void onCallback(Object result, Object object) {
//                super.onCallback(result, object);
//                clipText(((EditText)((Dialog) result).findViewById(123)).getText().toString());
//            }
//        });
    }


    public AlertDialog getEditDialog() {
        return editDialog;
    }

    boolean forint = false;

    public boolean isForNumber() {
        return forint;
    }

    public ShareAlert setForNumber(boolean forint) {
        this.forint = forint;
        return this;
    }

    @Show
    public Dialog shareEdit(final boolean isHint, final String model, final String txt, final FunCallback func, final String nega, final FunCallback leftfunc) {
        shareEditPrivate(isHint, model, txt, func, nega, leftfunc);
        return editDialog;
    }

    @SuppressLint("ResourceType")
    private void shareEditPrivate(final boolean isHint, final String model, final String txt, final FunCallback func, final String nega, final FunCallback leftfunc) {
        setHost(this);

        final Activity ctx = (Activity) getContext();

        final RelativeLayout rl = new RelativeLayout(ctx);
        rl.setLayoutParams(new ViewGroup.MarginLayoutParams(-1, -2));


//                final ScrollView view = new ScrollView(ctx);
//                view.setHorizontalScrollBarEnabled(true);
//                view.setSmoothScrollingEnabled(true);

        final EditText edit = new AppCompatEditText(ctx) {
            public boolean popEnable;

            //滑动距离的最大边界
            private int mOffsetHeight;

            //是否到顶或者到底的标志
            private boolean mBottomFlag = false;
            private boolean mCanVerticalScroll;

            @Override
            protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
                mCanVerticalScroll = canVerticalScroll();
            }

            @Override
            public boolean dispatchTouchEvent(MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN)
                    //如果是新的按下事件，则对mBottomFlag重新初始化
                    mBottomFlag = false;
                //如果已经不要这次事件，则传出取消的信号，这里的作用不大
                if (mBottomFlag)
                    event.setAction(MotionEvent.ACTION_CANCEL);
//                        getParent().requestDisallowInterceptTouchEvent(true);

                return super.dispatchTouchEvent(event);
            }

            @Override
            public boolean onTouchEvent(MotionEvent event) {
                boolean result = super.onTouchEvent(event);
                if (mCanVerticalScroll) {
                    //如果是需要拦截，则再拦截，这个方法会在onScrollChanged方法之后再调用一次
                    if (!mBottomFlag)
                        getParent().requestDisallowInterceptTouchEvent(true);
                } else {
                    if (getKeyListener() == null) {
                        getParent().requestDisallowInterceptTouchEvent(false);

                    } else {
//                                setCursorVisible(true);
                        requestFocus();
                        requestFocusFromTouch();
                        performClick();
                    }
                }
                return result;
            }

            @Override
            protected void onScrollChanged(int horiz, int vert, int oldHoriz, int oldVert) {
                super.onScrollChanged(horiz, vert, oldHoriz, oldVert);
                if (vert == mOffsetHeight || vert == 0) {
                    //这里触发父布局或祖父布局的滑动事件
                    getParent().requestDisallowInterceptTouchEvent(false);
                    mBottomFlag = true;
                }
            }

            /**
             * EditText竖直方向是否可以滚动
             *
             * @return true：可以滚动   false：不可以滚动
             */
            private boolean canVerticalScroll() {
                //滚动的距离
                int scrollY = getScrollY();
                //控件内容的总高度
                int scrollRange = getLayout().getHeight();
                //控件实际显示的高度
                int scrollExtent = getHeight() - getCompoundPaddingTop() - getCompoundPaddingBottom();
                //控件内容总高度与实际显示高度的差值
                mOffsetHeight = scrollRange - scrollExtent;

                if (mOffsetHeight == 0) {

                    return false;
                }

                return (scrollY > 0) || (scrollY < mOffsetHeight - 1);
            }

        };
        if (forint) {
            edit.setInputType(InputType.TYPE_CLASS_NUMBER);
        }

        if (!isHint) {
            editBackground = edit.getBackground();
            edit.setCursorVisible(false);
            edit.setBackgroundColor(Color.TRANSPARENT);

            edit.setText(model);
            MyClickListener.MyClickCallBack myClickCallBack;

            if (defaultEditFocused) {

            } else {
                edit.setOnTouchListener(new MyClickListener(myClickCallBack = new MyClickListener.MyClickCallBack() {

                                            @Override
                                            public void oneClick(View v, MotionEvent event, MyClickListener myClickListener) {
                                                if (edit.getKeyListener() == null) {
                                                    myClickListener.onTouch(v, event);

                                                }
                                            }

                                            @Override
                                            public void doubleClick(View v, MotionEvent event, MyClickListener myClickListener) {
                                                doubleClickPrivate((EditText) v);

                                            }
                                        }) {

                                            @Override
                                            public boolean onTouch(View v, MotionEvent event) {
                                                super.onTouch(v, event);
                                                if (popEnable) {
                                                    return false;
                                                }
                                                return true;
                                            }
                                        }
                );
            }
        } else {
            edit.setHint(model);
        }

        if (isDefaultEditFocused()) {
            edit.setCursorVisible(true);
            if (editBackground != null) {
                edit.setBackground(editBackground);
            }
            edit.requestFocusFromTouch();
            edit.requestFocus();
            edit.setSelection(0, edit.getText().toString().length());
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    InputMethodUtil.showInput(getContext(), edit);
                }
            }, 200);
        }
        TextView tv = null;
        if (doSelect != null) {

            rl.addView(
                    tv = TextViewBuilder.get(TextViewBuilder.class, getContext()).setText("选择").setResTextcolor(R.color.theme_color).setGravity(Gravity.CENTER).setWidth(-2).setHeight(-2)
                            .get()
            );
            tv.setPadding(0, DipUtil.fromDip(10), 0, 0);
            tv.setId(VerifyCode.getVerifycode());
//            tv.setBackgroundColor(Color.BLACK);
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final List list;
                    ShareAlert.get(ShareAlert.class, getContext()).listAlert(list = Arrays.asList("整型", "浮点", "上传", "日期"), new FunCallback() {
                        @Override
                        public void onCallback(Object result, Object object) {
                            super.onCallback(result, object);
                            doSelect.onCallback(result, edit);
                            ((Dialog) getParam()).dismiss();
                            ((Dialog) getParam()).cancel();
                        }


                    });
                }
            });
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(-2, -2);
            lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            lp.addRule(RelativeLayout.CENTER_VERTICAL);
            tv.setLayoutParams(lp);
        }


        RelativeLayout.LayoutParams elp = new RelativeLayout.LayoutParams(-1, -2);
        if (doSelect != null) {
            elp.addRule(RelativeLayout.LEFT_OF, tv.getId());

        }
        edit.setGravity(Gravity.LEFT);
        edit.setLayoutParams(elp);


        rl.addView(edit);


//                FlowLayout flowLayout = new FlowLayout(getContext());
//                TextView tv = new TextView(getContext());
//                ListBuilder lb = ListBuilder.build();
//
//                if (flowLayoutLabel != null && flowLayoutLabel.size() > 0) {
//                    for (String label : flowLayoutLabel) {
//                        lb.add(
//                                TextViewBuilder.get(TextViewBuilder.class, getContext()).setSizeValue(16).setHeight(-2).setWidth(-2).setText(label).get()
//                        );
//                    }
//                    flowLayout.setViews(ListBuilder.build().add(lb.get()).get());
//
//                }

//                view.addView(flowLayout);
//                rl.addView(flowLayout);

        edit.setId(123);
//                edit.setClickable(false);
//                edit.setKeyListener(null);
//                edit.setVerticalScrollBarEnabled(true);
//                edit.setKeyListener(null);
//                edit.setNestedScrollingEnabled(true);
//                edit.setScrollbarFadingEnabled(true);
        rl.setPadding(100, 100, 100, 30);

        new Handler(Looper.getMainLooper(), new Handler.Callback() {


            @SuppressLint("ResourceType")
            @Override
            public boolean handleMessage(Message msg) {
                if (editDialog != null) {
                    editDialog.cancel();
                    editDialog.dismiss();
                    editDialog = null;
                }
                editDialog = new AlertDialog.Builder(ctx).setView(rl).setCancelable(true)
                        .setNeutralButton(isHint || cancelshare ? "" : "分享", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Intent sendIntent = new Intent();
                                sendIntent.setAction(Intent.ACTION_SEND);
                                sendIntent.putExtra(Intent.EXTRA_TEXT, model);
                                sendIntent.setType("text/plain");
                                try {

                                    Intent chooserIntent = Intent.createChooser(sendIntent, "选择分享途径");
                                    if (chooserIntent == null) {
                                        return;
                                    }
                                    ctx.startActivity(chooserIntent);
                                } catch (Exception e) {
                                }
                                dialogInterface.dismiss();
                                dialogInterface.cancel();


                            }
                        }).setPositiveButton(txt, null)
                        .setNegativeButton(nega == null ? "  " : nega, null

                        ).create();
                editDialog.show();
                editDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String txt = ((TextView) v).getText().toString();
                        if (!TextUtils.isEmpty(txt) && txt.equals("编辑")) {
                            doubleClickPrivate(edit);
                            ((TextView) v).setText("完成");
                            return;
                        }
                        if (func == null) return;
                        func.simpleRun(func.getParam());
                        func.onCallback(editDialog, edit);

                    }
                });
                editDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if (leftfunc == null) return;
                        leftfunc.simpleRun(leftfunc.getParam());
                        leftfunc.onCallback(editDialog, edit);
                        editDialog.dismiss();
                        editDialog.cancel();
                    }
                });


                BroadcastBuilder.build().receive("posi_click", "", getContext(), new FunCallback() {
                    @Override
                    public void onCallback(Object result, Object object) {
                        super.onCallback(result, object);
                        String txt = ((TextView) editDialog.getButton(AlertDialog.BUTTON_POSITIVE)).getText().toString();

                        if (!TextUtils.isEmpty(txt) && txt.equals("编辑")) {
                            new Handler(Looper.getMainLooper(), new Handler.Callback() {
                                @Override
                                public boolean handleMessage(@NonNull Message msg) {
                                    ((TextView) editDialog.getButton(AlertDialog.BUTTON_POSITIVE)).setText("完成");
                                    return false;
                                }
                            }).sendEmptyMessage(0);
                        }
                        doubleClickPrivate(edit);
                    }
                });
                editDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        BroadcastBuilder.remove("posi_click", getContext());
                        if (closeActy) {
                            ((Activity) getContext()).finish();
                        }
                    }
                });
                if (editDialog.getButton(AlertDialog.BUTTON_POSITIVE).getText().equals("编辑")) {
                    if (editableEnabled) {
                        editDialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick();
                    }
                }
                if (cliboard) {
                    String paste = ClipUtil.get(ClipUtil.class, getContext()).paste();
                    if (!TextUtils.isEmpty(paste)) {
                        edit.setText(paste);
                    }
                }
//                dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
                return false;
            }
        }).sendEmptyMessage(0);
    }

    public void setHost(ShareAlert shareAlert) {
        this.host = shareAlert;
    }


    static Dialog dialog;

    @Show
    public static void shareAlert(final Activity ctx, final String model, final String txt, final FunCallback func) {
        shareAlertPrivate(ctx, model, null, txt, func);
    }

    private static void shareAlertPrivate(final Activity ctx, final String model, View view, final String txt, final FunCallback func) {
        new Handler(Looper.getMainLooper(), new Handler.Callback() {

            @Override
            public boolean handleMessage(Message msg) {
                if (dialog != null) {
                    dialog.cancel();
                    dialog.dismiss();
                    dialog = null;
                }
                AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
                if (!TextUtils.isEmpty(model)) {

                    builder.setMessage(model);
                } else if (view != null) {
                    builder.setView(view);
                }

                dialog = builder.setCancelable(true)
                        .setNeutralButton("分享", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Intent sendIntent = new Intent();
                                sendIntent.setAction(Intent.ACTION_SEND);
                                sendIntent.putExtra(Intent.EXTRA_TEXT, model);
                                sendIntent.setType("text/plain");
                                try {

                                    Intent chooserIntent = Intent.createChooser(sendIntent, "选择分享途径");
                                    if (chooserIntent == null) {
                                        return;
                                    }
                                    ctx.startActivity(chooserIntent);
                                } catch (Exception e) {
                                }
                                dialogInterface.dismiss();
                                dialogInterface.cancel();


                            }
                        }).setNegativeButton(txt, new DialogUtil.DialogClicker() {
                            @Override
                            public void onClick(DialogInterface dialog, int arg1) {
                                super.onClick(dialog, arg1);
                                func.simpleRun(func.getParam());
                            }
                        }).create();
                dialog.show();
                return false;
            }
        }).sendEmptyMessage(0);
    }

    boolean shouldShare = false;

    public ShareAlert setMessage(String model) {
        this.model = model;
        return this;
    }

    public ShareAlert setNeutralShare(boolean shouldShare) {
        this.shouldShare = shouldShare;
        return this;
    }

    public ShareAlert setNegativeTxt(String negativeTxt, FunCallback func) {
        this.negativeTxt = negativeTxt;
        setFunc(func);
        return this;
    }

    public ShareAlert setView(View view) {
        this.view = view;
        return this;
    }

    public void setFunc(FunCallback func) {
        this.func = func;
    }

    String negativeTxt;
    FunCallback func;

    public void show() {
        final Activity ctx = (Activity) getContext();
        new Handler(Looper.getMainLooper(), new Handler.Callback() {

            @Override
            public boolean handleMessage(Message msg) {
                if (dialog != null) {
                    dialog.cancel();
                    dialog.dismiss();
                    dialog = null;
                }
                AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
                if (!TextUtils.isEmpty(model)) {
                    builder.setMessage(model);
                } else if (view != null) {
                    builder.setView(view);
                }

                if (shouldShare) {
                    builder.setNeutralButton("分享", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Intent sendIntent = new Intent();
                            sendIntent.setAction(Intent.ACTION_SEND);
                            sendIntent.putExtra(Intent.EXTRA_TEXT, model);
                            sendIntent.setType("text/plain");
                            try {

                                Intent chooserIntent = Intent.createChooser(sendIntent, "选择分享途径");
                                if (chooserIntent == null) {
                                    return;
                                }
                                ctx.startActivity(chooserIntent);
                            } catch (Exception e) {
                            }
                            dialogInterface.dismiss();
                            dialogInterface.cancel();


                        }
                    });
                }
                if (!TextUtils.isEmpty(negativeTxt) && func != null) {
                    builder.setNegativeButton(negativeTxt, new DialogUtil.DialogClicker() {
                        @Override
                        public void onClick(DialogInterface dialog, int arg1) {
                            super.onClick(dialog, arg1);
                            func.simpleRun(func.getParam());
                            func.onCallback(func.getParam(), null);
                        }
                    });
                }
                dialog = builder.setCancelable(true)
                        .create();
                dialog.show();
                return false;
            }
        }).sendEmptyMessage(0);
    }

    @Show
    public static void shareAlert(final Activity ctx, final String model) {
        shareAlertPrivate(ctx, model);
    }

    private static void shareAlertPrivate(final Activity ctx, final String model) {
        new Handler(Looper.getMainLooper(), new Handler.Callback() {

            @Override
            public boolean handleMessage(Message msg) {
                if (dialog != null) {
                    if (dialog.getWindow() == null || !dialog.getWindow().isActive()) {
                        dialog = null;
                    } else {
                        dialog.cancel();
                        dialog.dismiss();
                        dialog = null;
                    }
                }
                dialog = new AlertDialog.Builder(ctx).setMessage(model).setCancelable(true).setNeutralButton("分享", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent sendIntent = new Intent();
                        sendIntent.setAction(Intent.ACTION_SEND);
                        sendIntent.putExtra(Intent.EXTRA_TEXT, model);
                        sendIntent.setType("text/plain");
                        try {

                            Intent chooserIntent = Intent.createChooser(sendIntent, "选择分享途径");
                            if (chooserIntent == null) {
                                return;
                            }
                            ctx.startActivity(chooserIntent);
                        } catch (Exception e) {
                        }
                        dialogInterface.dismiss();
                        dialogInterface.cancel();


                    }
                }).create();
                try {
                    dialog.show();
                } catch (Exception e) {

                }
                return false;
            }
        }).sendEmptyMessage(0);
    }


    @Show
    public static void sureAlert(final Activity ctx, final String model, final FunCallback surefuc) {
        sureAlert(ctx, model, model, surefuc);
    }

    @Show
    public static void sureAlert(final Activity ctx, final String model, final String sharemodel, final FunCallback surefuc) {
        sureAlertPrivate(ctx, model, sharemodel, surefuc);
    }

    private static void sureAlertPrivate(final Activity ctx, final String model, final String sharemodel, final FunCallback surefuc) {
        new Handler(Looper.getMainLooper(), new Handler.Callback() {

            @Override
            public boolean handleMessage(Message msg) {
                new AlertDialog.Builder(ctx).setMessage(model).setCancelable(true).setPositiveButton("", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent sendIntent = new Intent();
                        sendIntent.setAction(Intent.ACTION_SEND);
                        sendIntent.putExtra(Intent.EXTRA_TEXT, sharemodel);
                        sendIntent.setType("text/plain");
                        try {

                            Intent chooserIntent = Intent.createChooser(sendIntent, "选择分享途径");
                            if (chooserIntent == null) {
                                return;
                            }
                            ctx.startActivity(chooserIntent);
                        } catch (Exception e) {
                        }


                    }
                }).setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (surefuc != null) {
                            surefuc.simpleRun(null);
                        }
                        dialog.dismiss();
                        dialog.cancel();
                    }
                }).create().show();
                return false;
            }
        }).sendEmptyMessage(0);
    }

    public OneListView luil;

    public OneListView getLuil() {
        return luil;
    }

    @Show
    public ListView list2View(final Context ctx, final List model, final FunCallback funCallback) {
        OneListView oneListView = OneListView.get(OneListView.class, ctx);
        if (list_layoutid > 0) {
            oneListView.setLayoutid(list_layoutid);
        }
        luil = oneListView.setEmptystr(emptystr).buildListView(model, funCallback);
        luil.getListview().addHeaderView(LayoutInflater.from(getContext()).inflate(R.layout.auto_horizontal_line, null));
        ((ViewGroup) luil.getListview().getParent()).removeView(luil.getListview());

        return luil.getListview();
    }


    public boolean listAlertCancelable = true;
    public boolean listsearchEditEnabled = true;

    public ShareAlert setListsearchEditEnabled(boolean listsearchEditEnabled) {
        this.listsearchEditEnabled = listsearchEditEnabled;
        return this;
    }

    public ShareAlert setListCancelable(boolean listAlertCancelable) {
        this.listAlertCancelable = listAlertCancelable;
        return this;
    }

    public ShareAlert create(View view) {
        d = new AlertDialog.Builder(getContext()).setView(view).setCancelable(listAlertCancelable).create();
        return this;
    }

    public ShareAlert showDialog() {
        d.show();
        return this;
    }

    @Show
    public ShareAlert listAlert(final List model, final FunCallback funCallback) {
        listAlertPrivate(model, funCallback);
        return this;
    }

    List rawmodel = new ArrayList();
    List varmodel = new ArrayList();

    public List getRawmodel() {
        return rawmodel;
    }

    public void setRawmodel(List rawmodel) {
        this.rawmodel = rawmodel;
    }


    public List getVarmodel() {
        return varmodel;
    }

    public void setVarmodel(List varmodel) {
        this.varmodel = varmodel;
    }

    boolean slideOrPop;

    public ShareAlert setSlideOrPop(boolean slideOrPop) {
        this.slideOrPop = slideOrPop;
        return this;
    }

    boolean defaultEditFocused = true;

    public boolean isDefaultEditFocused() {
        return defaultEditFocused;
    }

    public ShareAlert setDefaultEditFocused(boolean defaultEditFocused) {
        this.defaultEditFocused = defaultEditFocused;
        return this;
    }

    Dialog d;


    private void listAlertPrivate(final List model, final FunCallback funCallback) {
        new Handler(Looper.getMainLooper(), new Handler.Callback() {

            @Override
            public boolean handleMessage(Message msg) {
                rawmodel.addAll(model);
                varmodel.addAll(model);
                oneListView = OneListView.get(OneListView.class, getContext());
                oneListView = oneListView.setEmptystr(emptystr).setDefaultClose(defaultClose).setModel(varmodel).buildListViewPre(funCallback);


                final ListView luil = oneListView.getListview();

//                if (addclickrun.get().size() > 0) {
//                    for (Object o : addclickrun.get().keySet()) {
//                        String k = o.toString();
//                        View footer = LayoutInflater.from(getContext()).inflate(R.layout.auto_string_item_choosable, null, false);
//                        luil.addFooterView(footer);
//                        ((TextView) footer.findViewById(R.id.name)).setText(k);
//                        footer.setOnClickListener(new View.OnClickListener() {
//                            @Override
//                            public void onClick(View view) {
//                                ((FunCallback) addclickrun.get().get(o)).onCallback(k, ShareAlert.this);
//                            }
//                        });
//
//                    }
//                }
                oneListView.setAdapter();
                if (title != null) {
                    ((TextView) ((View) luil.getParent()).findViewById(R.id.tv_title)).setText(title);

                    (((View) luil.getParent()).findViewById(R.id.top_bar_layout)).setVisibility(View.VISIBLE);
                }

                if (searchedithint != null) {
                    ((EditText) ((View) luil.getParent()).findViewById(R.id.edit)).setHint(searchedithint);
                }

                if (listsearchEditEnabled) {
                    ((View) luil.getParent()).findViewById(R.id.clear).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (((EditText) ((View) luil.getParent()).findViewById(R.id.edit)).getText().toString().equals("")) {
                                if (slideOrPop) {
                                    slideBottomUtil.dismiss();
                                } else {
                                    d.dismiss();
                                    d.cancel();
                                }
                            } else
                                ((EditText) ((View) luil.getParent()).findViewById(R.id.edit)).setText("");
                        }
                    });
                    ((EditText) ((View) luil.getParent()).findViewById(R.id.edit)).addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {

                        }

                        @Override
                        public void afterTextChanged(Editable s) {
//                            if(queryfunc!=null){
//
//                            }
                            String txt = s.toString();
//                            if (TextUtils.isEmpty(txt)) {
                            varmodel.clear();
                            varmodel.addAll(rawmodel);
//                                ((BaseAdapter) luil.getAdapter()).notifyDataSetChanged();
//                                return;
//                            }
                            List rt = new ArrayList();

                            for (Object mm : varmodel) {
                                String m = PinyinHelper.transfer(mm.toString());

                                if (m.toLowerCase().contains(s.toString().toLowerCase())) {
                                    rt.add(mm);
                                }
                            }
                            varmodel.clear();
                            varmodel.addAll(rt);
                            ((BaseAdapter) luil.getAdapter()).notifyDataSetChanged();
                        }
                    });
                } else {
                    ((View) luil.getParent()).findViewById(R.id.edit_layout).setVisibility(View.GONE);
                }
                View v;
                if (luil.getParent() != null) {
                    v = (View) luil.getParent();
                } else {
                    v = luil;
                }
                if (d != null) {
                    d.dismiss();
                    d.cancel();
                }
                if (slideOrPop) {
                    int h = ((Activity) getContext()).getWindowManager().getDefaultDisplay().getHeight() / 5 * 3;
                    slideBottomUtil = SlideBottomUtil.slideBottom((Activity) getContext(), v, h);
                } else {
                    d = new AlertDialog.Builder(getContext()).setView(v).setCancelable(listAlertCancelable).create();
                }
                if (funCallback != null) {
                    funCallback.setParam(d != null ? d : slideBottomUtil);
                }
                if (TextUtils.isEmpty(click1text) && TextUtils.isEmpty(click2text)) {
                    oneListView.getOneview().findViewById(R.id.footer).setVisibility(View.GONE);
                } else {
                    if (TextUtils.isEmpty(click1text)) {
                        oneListView.getOneview().findViewById(R.id.footer).findViewById(R.id.footer1).setVisibility(View.GONE);
                    }
                    if (TextUtils.isEmpty(click2text)) {
                        oneListView.getOneview().findViewById(R.id.footer).findViewById(R.id.footer2).setVisibility(View.GONE);
                    }
                    oneListView.getOneview().findViewById(R.id.footer).setVisibility(View.VISIBLE);
                    TextView tv1 = (oneListView.getOneview().findViewById(R.id.footer1).findViewById(R.id.name));
                    TextView tv2 = (oneListView.getOneview().findViewById(R.id.footer2).findViewById(R.id.name));

                    tv1.setText(click1text);
                    tv2.setText(click2text);
                    tv1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            click1Run.onCallback(((EditText) ((View) luil.getParent()).findViewById(R.id.edit)).getText().toString(), d != null ? d : slideBottomUtil);
                        }
                    });
                    tv2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            click2Run.onCallback(((EditText) ((View) luil.getParent()).findViewById(R.id.edit)).getText().toString(), d != null ? d : slideBottomUtil);
                        }
                    });
                }
                luil.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Object o = parent.getItemAtPosition(position);

                        if (funCallback != null) {
                            if (d != null) {
                                funCallback.setParam(d);
                            } else {
                                funCallback.setParam(slideBottomUtil);

                            }
                            funCallback.onCallback(o, position);

                        }
                    }
                });


//                ((ViewGroup)d.getWindow().getDecorView().getParent()).removeAllViews();
                if (slideOrPop) {

                    slideBottomUtil.show((Activity) getContext());
                } else {

                    d.show();
                    Window window = d.getWindow();
                    if (window != null) {
                        WindowManager.LayoutParams lp = window.getAttributes();
                        lp.gravity = Gravity.CENTER;
                        DipUtil.initCtx(getContext());
                        lp.width = DipUtil.fromDip(300);//宽高设置具体大小
                        d.getWindow().setAttributes(lp);
                    }
                }
                if (slideOrPop) {
                    delayCallback();

                }
                return false;
            }
        }).sendEmptyMessage(0);
    }

    public String click1text;
    public FunCallback click1Run;
    public String click2text;
    public FunCallback click2Run;


    public ShareAlert addClick1Run(String text, FunCallback funCallback) {
        click1text = text;
        click1Run = funCallback;
        return this;
    }

    public ShareAlert addClick2Run(String text, FunCallback funCallback) {
        click2text = text;
        click2Run = funCallback;
        return this;
    }

    private void delayCallback() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (slideBottomUtil.isShowing()) {
                    if (listsearchEditEnabled) {
                        ((EditText) slideBottomUtil.getContentView().findViewById(R.id.edit)).requestFocusFromTouch();
                        ((EditText) slideBottomUtil.getContentView().findViewById(R.id.edit)).requestFocus();
                        ((EditText) slideBottomUtil.getContentView().findViewById(R.id.edit)).performClick();
                        InputMethodUtil.showInput(getContext(), ((EditText) slideBottomUtil.getContentView().findViewById(R.id.edit)));
                    }
                    if (showfunc != null) {
                        showfunc.onCallback(ShareAlert.this, null);
                    }
                } else {
                    delayCallback();
                }

            }
        }, 200);
    }

    @Show
    public static void choiceAlert(final Activity ctx, final String model, final FunCallback yfunc, final FunCallback nfunc) {
        choiceAlertPrivate(ctx, model, yfunc, nfunc);
    }

    private static void choiceAlertPrivate(final Activity ctx, final String model, final FunCallback yfunc, final FunCallback nfunc) {
        new Handler(Looper.getMainLooper(), new Handler.Callback() {

            @Override
            public boolean handleMessage(Message msg) {
                new AlertDialog.Builder(ctx).setMessage(model).setCancelable(true).setNegativeButton("否", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (nfunc != null) {
                            nfunc.simpleRun(null);
                        }
                        dialog.dismiss();
                        dialog.cancel();
                    }
                }).setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (yfunc != null) {
                            yfunc.simpleRun(null);
                        }
                        dialog.dismiss();
                        dialog.cancel();
                    }
                }).create().show();
                return false;
            }
        }).sendEmptyMessage(0);
    }

    @Show
    public static void sureAlertOnly(final Activity ctx, final String model, final FunCallback surefuc) {
        sureAlertPrivate(ctx, model, surefuc);
    }

    private static void sureAlertPrivate(final Activity ctx, final String model, final FunCallback surefuc) {
        new Handler(Looper.getMainLooper(), new Handler.Callback() {

            @Override
            public boolean handleMessage(Message msg) {
                new AlertDialog.Builder(ctx).setMessage(model).setCancelable(true).setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (surefuc != null) {
                            surefuc.simpleRun(null);
                        }
                        dialog.dismiss();
                        dialog.cancel();
                    }
                }).create().show();
                return false;
            }
        }).sendEmptyMessage(0);
    }

    static AlertDialog ad = null;


    @Show
    public static void delAlert(final Activity ctx, int spacecount, final FunCallback fcb) {
        delAlert(ctx, spacecount, fcb, null);
    }

    @Show
    public static void delAlert(final Activity ctx, final FunCallback fcb) {
        delAlert(ctx, 11, fcb, null);
    }

    @Show
    public static void delAlert(final Activity ctx, String txt, final FunCallback fcb) {
        delAlert(ctx, txt, 11, fcb, null);
    }


    @Show
    public static void delAlert(final Activity ctx, final FunCallback fcb, final FunCallback dimisscb) {
        delAlert(ctx, 11, fcb, null);
    }

    @Show
    public static void delAlert(final Activity ctx, final int spacecount, final FunCallback fcb, final FunCallback dimisscb) {
        delAlert(ctx, "删除", spacecount, fcb, dimisscb);
    }

    @Show
    public static void delAlert(final Activity ctx, final String txt, final int spacecount, final FunCallback fcb, final FunCallback dimisscb) {
        delAlertPrivate(ctx, txt, spacecount, fcb, dimisscb);
    }

    public static void textCenterAlert(final Activity ctx, final String txt, final int spacecount, final FunCallback fcb, final FunCallback dimisscb) {
        if (ad != null) {
            ad.dismiss();
            ad.cancel();
            ad = null;
        }


        new Handler(Looper.getMainLooper(), new Handler.Callback() {

            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public boolean handleMessage(Message msg) {
                String space = "";
                for (int i = 0; i < spacecount / 2; i++) {
                    space += "\u3000";
                }
                ad =
                        new AlertDialog.Builder(ctx).setNeutralButton(space + txt + space, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                                dialogInterface.cancel();
                                ad = null;
                                fcb.simpleRun(null);
                                fcb.onCallback(null, null);
                            }
                        }).setCancelable(true).create();

                ad.show();
                ad.getButton(AlertDialog.BUTTON_NEUTRAL).setTextColor(ctx.getColor(R.color.theme_color));
                DipUtil.initCtx(ctx);
                ad.getButton(AlertDialog.BUTTON_NEUTRAL).setTextSize(DipUtil.fromDip(5));

                ad.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        if (dimisscb != null) {
                            dimisscb.onCallback(null, null);
                        }
                    }
                });
                return false;
            }
        }).sendEmptyMessage(0);
    }

    private static void delAlertPrivate(final Activity ctx, final String txt, final int spacecount, final FunCallback fcb, final FunCallback dimisscb) {
        if (ad != null) {
            ad.dismiss();
            ad.cancel();
            ad = null;
        }


        new Handler(Looper.getMainLooper(), new Handler.Callback() {

            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public boolean handleMessage(Message msg) {
                String space = "";
                for (int i = 0; i < spacecount; i++) {
                    space += "\u3000";
                }
                ad =
                        new AlertDialog.Builder(ctx).setNeutralButton(txt + space, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                                dialogInterface.cancel();
                                ad = null;
                                fcb.simpleRun(null);
                                fcb.onCallback(null, null);
                            }
                        }).setCancelable(true).create();

                ad.show();
                ad.getButton(AlertDialog.BUTTON_NEUTRAL).setTextColor(ctx.getColor(R.color.theme_color));
                ad.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        if (dimisscb != null) {
                            dimisscb.onCallback(null, null);
                        }
                    }
                });
                return false;
            }
        }).sendEmptyMessage(0);
    }

    @Override
    public AndroidInstance use(Object mp, Object... p) {
        return null;
    }

    public ShareAlert setOnShow(FunCallback funCallback) {
        showfunc = funCallback;
        return this;
    }

    public ShareAlert addreplaceEmpty(String aPackage) {
        this.emptystr = aPackage;
        return this;
    }

    public ShareAlert setDefaultClose(boolean b) {
        this.defaultClose = b;
        return this;
    }


    public ShareAlert setCancelShare(boolean cancelshare) {
        this.cancelshare = cancelshare;
        return this;
    }

    public boolean isCancelshare() {
        return cancelshare;
    }

//    public boolean editclicked = false;


}
